import { createRouter, createWebHistory, createWebHashHistory } from 'vue-router';
import { useUserStore } from '@/store';
import { addRoutes, clerRoutes } from '@/util/anyncRoutes';

const routes = [
    {
        path: '/',
        name: 'working',
        redirect: '/happenNumWarning',
        meta: {
            title: '正在开发...',
            layout: true,
            menu: true,
            icon: 'DashboardOutlined',
            sort: 1,
        },
    },
    {
        path: '/test',
        name: 'test',
        component: () => import('@/views/testDemo/index.vue'),
        meta: {
            title: '测试页面',
            layout: true,
            menu: true,
            icon: 'DashboardOutlined',
            sort: 1,
        },
    },
    {
        path: '/dataSourceManage',
        name: 'dataSourceManage',
        component: () => import('@/views/DataSourceManage/index.vue'),
        meta: {
            title: '数据源管理',
            layout: true,
            menu: true,
            icon: 'DashboardOutlined',
            sort: 1,
        },
    },
    {
        path: '/importTplConfig',
        name: 'importTplConfig',
        component: () => import('@/views/ImportTplConfig/index.vue'),
        meta: {
            title: '导入模板配置',
            layout: true,
            menu: true,
            icon: 'DashboardOutlined',
            sort: 1,
        },
    },
    {
        path: '/reportDataManage',
        name: 'reportDataManage',
        component: () => import('@/views/ReportDataManage/index.vue'),
        meta: {
            title: '报表数据管理',
            layout: true,
            menu: true,
            icon: 'DashboardOutlined',
            sort: 1,
        },
    },
    {
        path: '/treasuryWarningPhoto',
        name: 'treasuryWarningPhoto',
        component: () => import('@/views/TreasuryWarningPhoto/index.vue'),
        meta: {
            title: '库款预警画像',
            layout: true,
            menu: true,
            icon: 'DashboardOutlined',
            sort: 1,
        },
    },
    {
        path: '/provinceFundWarning',
        name: 'provinceFundWarning',
        redirect: '/provinceFundWarning/provinceFundWarningMonth',
        component: () => import('@/views/TreasuryWarningPhoto/provinceFundWarning.vue'),
        meta: {
            title: '全省资金预警',
            layout: true,
            menu: true,
            icon: 'DashboardOutlined',
            sort: 1,
        },
        children: [
            {
                path: 'provinceFundWarningMonth',
                name: 'provinceFundWarningMonth',
                component: () => import('@/views/TreasuryWarningPhoto/provinceFundWarningMonth.vue'),
                meta: {
                    title: '全省资金预警（月）',
                    layout: true,
                    menu: true,
                    icon: 'DashboardOutlined',
                    sort: 1,
                },
            },
            {
                path: 'provinceFundWarningDay',
                name: 'provinceFundWarningDay',
                component: () => import('@/views/TreasuryWarningPhoto/provinceFundWarningDay.vue'),
                meta: {
                    title: '全省资金预警（日）',
                    layout: true,
                    menu: true,
                    icon: 'DashboardOutlined',
                    sort: 1,
                },
            },
        ],
    },
    {
        path: '/forecastNumWarning',
        name: 'forecastNumWarning',
        component: () => import('@/views/TreasuryWarningPhoto/forecastNumWarning.vue'),
        meta: {
            title: '预测数预警（月）',
            layout: true,
            menu: true,
            icon: 'DashboardOutlined',
            sort: 1,
        },
    },
    {
        path: '/happenNumWarning',
        name: 'happenNumWarning',
        component: () => import('@/views/TreasuryWarningPhoto/happenNumWarning.vue'),
        meta: {
            title: '发生数预警（日）',
            layout: true,
            menu: true,
            icon: 'DashboardOutlined',
            sort: 1,
        },
    },
    {
        path: '/:pathMatch(.*)',
        name: '404',
        component: () => import('@/views/404/index.vue'),
        meta: {
            title: '404',
            layout: true,
            menu: true,
            icon: 'DashboardOutlined',
            sort: 1,
        },
    },
];

const router = createRouter({
    // history: createWebHistory(),
    history: createWebHashHistory('inc-analysis-web'),
    routes,
});

let registerRouteFresh = true;
router.beforeEach((to, from, next) => {
    const userStore = useUserStore();
    if (to.meta.title) {
        document.title = to.meta.title as string;
    }
    // if (to.name !== 'Login' && !userStore.token) {
    //     next();
    // } else {

    //     if (!from.name && registerRouteFresh) {
    //         addRoutes(userStore, router);
    //         next({ ...to, replace: true });
    //         registerRouteFresh = false;
    //     } else {
    //         next();
    //     }
    // }
    next();
});

export default router;
